package com.eat.fish_service.config.mpConfig;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.eat.servicebase.handler.MyMetaObjectHandler;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author CZF
 * @create 2022-05-13 14:48
 *
 * 主数据源配置，业务数据库的数据源配置
 */

@Configuration
public class MainManager {

    @Autowired
    MybatisPlusInterceptor mybatisPlusInterceptor;

    @Autowired
    MyMetaObjectHandler myMetaObjectHandler;

    @Value("${mybatis-plus.mapper-locations}")
    String mapperLoacation;

    public MainManager() {
    }

    //设置数据源
    @Bean(name = {"mainDataSource"})
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.main")
    @Primary
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    //主数据源事务管理
    @Bean(name = {"mainTransMg"})
    @Primary
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(this.dataSource());
    }

    //创建sqlSessionFactory
    @Bean(name = {"mainSqlSessionFactory"})
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("mainDataSource") DataSource dataSource) throws Exception {
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setLogImpl(StdOutImpl.class);
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();

        sessionFactory.setPlugins(mybatisPlusInterceptor);  //加入mybatisplus插件
        sessionFactory.setConfiguration(configuration);
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLoacation));

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(myMetaObjectHandler);  //加入自定义的mybatisplus 处理器
        sessionFactory.setGlobalConfig(globalConfig);

        return sessionFactory.getObject();
    }

    //mybatis-plus的GlobalConfig
//    @Bean(name = {"globalConfig"})
//    @Primary
//    public GlobalConfig setMpGlobalConfig(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
//        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setMetaObjectHandler(myMetaObjectHandler);
//        globalConfig.setSqlSessionFactory(sqlSessionFactory);
//        return globalConfig;
//    }

    //创建sqlSession模板
    @Bean(name = "mainSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("mainSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
